<div class="dataset-compliance-fields__subtitle">
  {{#if fieldReviewHint}}
    <div class="dataset-compliance-fields__review-hint">
      {{fieldReviewHint}}
    </div>
  {{/if}}
</div>

{{#if showGuidedComplianceEditMode}}
  {{#if foldedChangeSet.length}}
    {{#dataset-table
      class="dataset-compliance-fields"
      fields=foldedChangeSet
      filterBy=filterBy
      tableRowComponent='dataset-compliance-rollup-row'
      searchTerm=searchTerm as |table|
    }}

      {{#table.head as |head|}}
        {{#head.column class="dataset-compliance-fields__notification-column"}}{{/head.column}}
        {{#head.column class="dataset-compliance-fields__identifier-column"}}
          Field

          {{more-info
            link=@wikiLinks.tmsSchema
            tooltip="Click for more information on Schema"
          }}
        {{/head.column}}
        {{#head.column class="nacho-table-cell-wrapped"}}Compliance Information{{/head.column}}
        {{#head.column class="nacho-table-cell-wrapped"}}
          System Suggestion & Confidence
        {{/head.column}}
      {{/table.head}}

      <tr>
        <th>{{!--spacer--}}</th>
        <th colspan="3">
          <div class="dataset-compliance-fields__actions">
            {{disable-bubble-input
              title="Search field names"
              placeholder="Search field names"
              value=table.searchTerm
              class="dataset-compliance-fields__search"
              on-input=(action table.filterDidChange value="target.value")
            }}
          </div>
        </th>
      </tr>

      {{#table.body as |body|}}
        {{#each table.data as |field|}}
          {{#body.row
            field=field
            isNewComplianceInfo=isNewComplianceInfo
            complianceDataTypes=complianceDataTypes
            suggestionConfidenceThreshold=suggestionConfidenceThreshold
            onFieldDblClick=(action "onFieldDblClick")
            onFieldTagAdded=(action "onFieldTagAdded")
            onFieldTagRemoved=(action "onFieldTagRemoved")
            onTagReadOnlyDisable=(action "onTagReadOnlyDisable")
            onTagIdentifierTypeChange=(action "tagIdentifierChanged")
            onSuggestionIntent=(action "onFieldSuggestionIntentChange") as |row|
          }}
            {{! template-lint-disable no-invalid-interactive }}
            <tr class="{{if row.isReadonly 'dataset-compliance-fields--readonly'}}" ondblclick={{action
              row.onFragmentDblClick}}>
              {{#row.cell}}
                {{! primary tag has an identifierType, field has a suggestion that does not match the identifierType, and suggestion resolution}}
                {{#if
                  (and row.fieldProps.identifierType (and row.suggestion (and (not row.suggestionMatchesCurrentValue) (not row.suggestionResolution))))}}

                  <span class="nacho-tooltip" data-title="Suggestion conflicts with data type">
                    <i class="fa fa-exclamation dataset-compliance-fields__has-suggestions__icon"
                       title="Compliance field has suggested values"></i>
                  </span>

                {{else}}

                  {{#if row.isReviewRequested}}

                    <span class="nacho-tooltip" data-title="Please review">
                      <i class="fa fa-question dataset-compliance-fields--review-required__icon"
                         title="Compliance policy information needs review"></i>
                    </span>

                  {{else}}

                    <span class="nacho-tooltip" data-title="All good!">
                      <i class="fa fa-check dataset-compliance-fields--ok__icon" title="All good!"></i>
                    </span>


                  {{/if}}

                {{/if}}
              {{/row.cell}}

              {{#row.cell}}
                <div class="dataset-compliance-fields__id-field-wrap">
                  <div title="{{row.identifierField}}">
                    <strong>
                      {{if isShowingFullFieldNames row.identifierField (split-text row.identifierField 23)}}
                    </strong>
                  </div>

                  <div title="{{row.dataType}}">
                    {{if isShowingFullFieldNames row.dataType (split-text row.dataType 23)}}
                  </div>
                </div>
              {{/row.cell}}

              {{#row.cell}}
                {{#if (and isEditing (not row.isReadonly))}}
                  {{#each row.fieldChangeSet as |tag|}}

                    <div class="dataset-compliance-fields__tag-item">
                      {{#basic-dropdown as |tagDrop|}}
                        {{#tagDrop.trigger
                          class="dataset-compliance-fields__tag-info dataset-compliance-fields__tag-info--editable"}}

                          <p class="dataset-compliance-fields__tag-info__text">
                            {{#if tag.identifierType}}
                              {{tag.identifierType}}{{if tag.logicalType (concat " (" tag.logicalType ")")}}
                            {{else}}
                              <span
                                class="dataset-compliance-fields__tag-info__text dataset-compliance-fields__tag-info__text--obscure">
                                Select Field Type ...
                              </span>
                            {{/if}}
                          </p>

                        {{/tagDrop.trigger}}

                        {{#tagDrop.content overlay=true class="dataset-compliance-fields__guided-modal"}}
                          {{#dataset-compliance-field-tag
                            sourceTag=tag
                            parentHasSingleTag=row.hasSingleTag
                            tagDidChange=(action "tagPropertiesUpdated")
                            suggestionConfidenceThreshold=suggestionConfidenceThreshold
                            complianceFieldIdDropdownOptions=complianceFieldIdDropdownOptions
                            complianceDataTypes=complianceDataTypes as |tagRowComponent|
                          }}
                            <article class="dataset-compliance-fields__field-tag__content">
                              <section class="dataset-compliance-fields__compliance-info-column">
                                <header class="dataset-compliance-fields__compliance-info-column__title">
                                  <strong>Select field type</strong>
                                </header>

                                <div class="dataset-compliance-fields__compliance-info-column__content">
                                  {{#track-ui-event category=trackableCategory.Compliance
                                                    action=trackableEvent.Compliance.FieldIndentifier
                                                    name=tagRowComponent.tag.identifierType as |metrics|}}

                                    {{#each tagRowComponent.tagIdOptions as |tagOption|}}
                                      {{#radio-button-composer
                                        value=tagOption.value
                                        name="datasetFieldClassification"
                                        groupValue=(readonly tagRowComponent.tag.identifierType)
                                        class="dataset-compliance-fields__tag-radio"
                                        disabled=tagOption.isDisabled
                                        disabledClass="dataset-compliance-fields__tag-radio--disabled"
                                        checkedClass="dataset-compliance-fields__tag-radio--checked"
                                        onMouseEnter=(action tagRowComponent.onFieldTagIdentifierEnter)
                                        onMouseLeave=(action tagRowComponent.onFieldTagIdentifierLeave)
                                        changed=(action metrics.trackOnAction (action tagRowComponent.tagIdentifierTypeDidChange))}}
                                        {{tagOption.label}}
                                      {{/radio-button-composer}}
                                    {{/each}}

                                  {{/track-ui-event}}
                                </div>
                              </section>

                              {{#if tagRowComponent.fieldIdQuickDesc}}

                                <section class="dataset-compliance-fields__compliance-info-column">
                                  <div class="dataset-compliance-fields__field-tag__quick-desc">
                                    <strong>
                                      {{tagRowComponent.fieldIdQuickDesc.title}}:
                                    </strong>

                                    <p>
                                      {{tagRowComponent.fieldIdQuickDesc.description}}
                                    </p>
                                  </div>
                                </section>

                              {{else}}

                                {{#if tagRowComponent.isIdType}}
                                  <section class="dataset-compliance-fields__compliance-info-column">
                                    <header class="dataset-compliance-fields__compliance-info-column__title">
                                      <strong>Select field format</strong>
                                    </header>

                                    <div class="dataset-compliance-fields__compliance-info-column__content">
                                      {{#track-ui-event category=trackableCategory.Compliance
                                                        action=trackableEvent.Compliance.FieldFormat
                                                        name=tagRowComponent.tag.logicalType as |metrics|}}

                                        {{#each tagRowComponent.fieldFormats as |fieldFormat|}}
                                          {{#radio-button-composer
                                            value=fieldFormat.value
                                            name="datasetFieldFieldFormat"
                                            groupValue=(readonly tagRowComponent.tag.logicalType)
                                            class="dataset-compliance-fields__tag-radio"
                                            checkedClass="dataset-compliance-fields__tag-radio--checked"
                                            onMouseEnter=(action tagRowComponent.onFieldFormatEnter)
                                            onMouseLeave=(action tagRowComponent.onFieldFormatLeave)
                                            changed=(action metrics.trackOnAction (action tagRowComponent.tagLogicalTypeDidChange))}}
                                            {{fieldFormat.label}}
                                          {{/radio-button-composer}}
                                        {{/each}}

                                      {{/track-ui-event}}
                                    </div>
                                  </section>
                                  {{#if tagRowComponent.fieldFormatQuickDesc}}

                                    <section class="dataset-compliance-fields__compliance-info-column">
                                      <div class="dataset-compliance-fields__field-tag__quick-desc">
                                        <strong>
                                          {{tagRowComponent.fieldFormatQuickDesc.title}}:
                                        </strong>

                                        <p>
                                          {{tagRowComponent.fieldFormatQuickDesc.description}}
                                        </p>
                                      </div>
                                    </section>

                                  {{else}}

                                    {{#if tagRowComponent.showCustomInput}}
                                      <section class="dataset-compliance-fields__compliance-info-column">
                                        <header class="dataset-compliance-fields__compliance-info-column__title">
                                          <strong>Custom RegEx</strong>
                                        </header>

                                        <div
                                          class="dataset-compliance-fields__compliance-info-column__content">

                                          <div class="dataset-compliance-fields__text-pattern-wrap">
                                            <div class="dataset-compliance-fields__text-pattern-wrap--input">
                                              <input
                                                placeholder="Enter a RegExp"
                                                value="{{readonly tagRowComponent.tag.valuePattern}}"
                                                disabled={{or (not isEditing) row.isReadonly}}
                                                  class="dataset-compliance-fields__text-pattern {{if
                                              tagRowComponent.valuePatternError
                                              'dataset-compliance-fields--missing-selection'}}"
                                              oninput={{action tagRowComponent.tagValuePatternDidChange
                                                               value="target.value"}}
                                              >
                                            </div>

                                            {{more-info
                                              link=@wikiLinks.metadataCustomRegex
                                              tooltip="Click for more information on RegExp format"
                                            }}

                                            {{#if tagRowComponent.valuePatternError}}
                                              <div class="dataset-compliance-fields__text-pattern-wrap--error">
                                                {{tagRowComponent.valuePatternError}}
                                              </div>
                                            {{/if}}
                                          </div>
                                        </div>
                                      </section>
                                    {{/if}}

                                    {{#unless tagRowComponent.isTagFormatMissing}}
                                      <section class="dataset-compliance-fields__compliance-info-column">
                                        <header class="dataset-compliance-fields__compliance-info-column__title">
                                          <strong>Is purge key?</strong>
                                          {{more-info
                                            link=@wikiLinks.purgeKey
                                            tooltip="Click for more information about purge keys"
                                          }}
                                        </header>

                                        <div class="dataset-compliance-fields__compliance-info-column__content">

                                          {{#radio-button-composer
                                            value=false
                                            name="datasetFieldOwnerToggle"
                                            groupValue=(readonly tagRowComponent.tag.nonOwner)
                                            class="dataset-compliance-fields__tag-radio"
                                            checkedClass="dataset-compliance-fields__tag-radio--checked"
                                            changed=(action tagRowComponent.tagOwnerDidChange)}}
                                            Yes
                                          {{/radio-button-composer}}

                                          {{#radio-button-composer
                                            value=true
                                            name="datasetFieldOwnerToggle"
                                            groupValue=(readonly tagRowComponent.tag.nonOwner)
                                            class="dataset-compliance-fields__tag-radio"
                                            checkedClass="dataset-compliance-fields__tag-radio--checked"
                                            changed=(action tagRowComponent.tagOwnerDidChange)}}
                                            No
                                          {{/radio-button-composer}}
                                        </div>
                                      </section>
                                    {{/unless}}

                                  {{/if}}
                                {{/if}}
                              {{/if}}
                            </article>

                            <footer class="dataset-compliance-fields__field-tag__footer">
                              {{#if tagRowComponent.localTagHasDiverged}}
                                <button
                                  class="nacho-button nacho-button--large-inverse"
                                  onclick={{action tagRowComponent.onFieldTagUpdate tagDrop.actions.close}}
                                    disabled={{tagRowComponent.isTagReviewRequired}}>
                                  Done
                                </button>
                              {{/if}}
                            </footer>
                          {{/dataset-compliance-field-tag}}
                        {{/tagDrop.content}}
                      {{/basic-dropdown}}

                      {{#if (and isEditing (not row.hasSingleTag))}}
                        <span class="nacho-tooltip" data-title="Delete Tag">
                          <button class="nacho-button nacho-button--tertiary dataset-compliance-fields__remove-tag"
                                  onclick={{action row.onRemoveFieldTag tag}}>
                            {{fa-icon "trash"}}
                          </button>
                        </span>
                      {{/if}}
                    </div>
                  {{/each}}

                  {{#unless row.hasNoneTag}}
                    <button
                      class="nacho-button nacho-button--tertiary dataset-compliance-fields__add-field"
                      onclick={{action row.onAddFieldTag}}>
                      + Add More
                    </button>
                  {{/unless}}

                {{else}}
                  {{#each row.fieldChangeSet as |tag|}}

                    <div class="dataset-compliance-fields__tag-info">
                      {{tag.identifierType}}{{if tag.logicalType (concat " (" tag.logicalType ")")}}

                      {{#if row.isReadonly}}
                        <span class="nacho-tooltip" data-title="Readonly">
                          <button class="nacho-button nacho-button--tertiary dataset-compliance-fields--readonly__icon"
                                  disabled={{not isEditing}}
                                  onclick={{action row.onEditReadonlyTag tag}}>
                            {{fa-icon "lock"}}
                          </button>
                        </span>
                      {{/if}}
                    </div>

                  {{/each}}
                {{/if}}
              {{/row.cell}}

              {{#row.cell}}
                <div class="dataset-compliance-fields__suggested-values">
                  {{#if row.suggestion}}
                    <span
                      class="dataset-compliance-fields__suggested-value {{unless row.suggestionResolution
                                                                                 'dataset-compliance-fields__suggested-value--no-res'}}"
                      title="{{row.suggestion.identifierType}} ({{row.suggestion.confidence}}%)">
                      {{row.suggestion.identifierType}} ({{row.suggestion.confidence}}%)
                    </span>

                    {{#if isEditing}}
                      <div class="dataset-compliance-fields__suggestion-actions">
                        {{#if row.suggestionResolution}}
                          <div
                            class="dataset-compliance-fields__resolution {{if (eq row.suggestionResolution 'Accepted')
                                                                              'dataset-compliance-fields__resolution--ok'}}">
                            {{row.suggestionResolution}}
                          </div>
                        {{else}}
                          {{auto-suggest-action
                            type="accept"
                            field=row.fieldProps
                            feedbackAction=(action notifyOnComplianceSuggestionFeedback)
                            onSuggestionClick=(action row.onSuggestionClick)
                          }}

                          {{auto-suggest-action
                            field=row.fieldProps
                            feedbackAction=(action notifyOnComplianceSuggestionFeedback)
                            onSuggestionClick=(action row.onSuggestionClick)
                          }}
                        {{/if}}
                      </div>
                    {{/if}}
                  {{else}}
                    &mdash;
                  {{/if}}
                </div>
              {{/row.cell}}
            </tr>

          {{/body.row}}
        {{/each}}
      {{/table.body}}

    {{/dataset-table}}

  {{else}}

    {{empty-state
      heading="No fields found"
      subHead="If you have a filter applied, setting this to the least restrictive option may yield more results."
    }}

  {{/if}}
{{else}}

  {{ember-ace
    readOnly=isReadOnly
    update=(action "onManualComplianceUpdate")
    class="dataset-compliance-editor"
    value=jsonComplianceEntities
    enableAutocompletion=true
    enableLiveAutocompletion=true
    minLines=10
    maxLines=50
    showLineNumbers=true
    useWrapMode=true
    mode="ace/mode/json"
    worker="ace/mode/json_worker"
    theme="ace/theme/github"}}

{{/if}}
